Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

163
Visualizações
How to throw "expressive" / console friendly exceptions in JavaScript?

JavaScript noob here... I'm exploring various options for throwing exceptions.

Throwing a string / primitive value

This seems to be bad practice for several reasons (catching, stacktrace, ...)

Using Error

throw new Error("bad times")

This seems to be a common approach, but I'm not a big fan because I can't easily add extra information. I can't for example do

throw new Error("bad times", { reason: strangeObject })

I could JSON.stringify the extra information, but I like when I can expand / collapse the object in the console.

From what I can tell I won't get any farther with custom exceptions / Error-subclasses.

Throwing { msg: "bad times", reason: strangeObject }

This allows me to expand / collapse strangeObject nicely in the console. For exceptions throughout my application to be consistent, I'd need to repeat the msg: part everywhere, which I'm not a fan of.

I ended up going for the last approach, just to find out that the ESLinter complains about this approach:

Expected an error object to be thrown no-throw-literal

So, my question is basically: Can I use Error (or custom Error subclasses) in a console-friendly way, i.e. support expand/collapse?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I've settled for a custom exception and a window.onerror handler:

class MyAppError extends Error {
  constructor(msg, details) {
    super(msg)
    this.details = details
  }
}

window.onerror = function(message, source, lineno, colno, error) {
    if (error instanceof MyAppError) {
        console.error(message, error.details)
        return true
    }
    return false
}

function throwSomething() {
    throw new MyAppError("I made a mess", {
        someDetail: "a value",
        otherStuff: "another value"
    });
}

console.log("Hello World");
throwSomething();

(JSFiddle)

Result looks like this in Firefox and Chrome, which I'm quite happy with.

enter image description here

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda